package com.twentytwok.hospitalsystem22K.service.impl;

import com.twentytwok.hospitalsystem22K.common.Code;
import com.twentytwok.hospitalsystem22K.common.R;
import com.twentytwok.hospitalsystem22K.mapper.IndexOpenLogMapper;
import com.twentytwok.hospitalsystem22K.pojo.IndexOpenLog;
import com.twentytwok.hospitalsystem22K.service.IndexOpenLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * IndexOpenLogServiceImpl.java
 * HospitalSystem22K
 *
 * @author Yuanao
 * @date 2021/11/19 20:13
 */
@Service("indexOpenLogService")
public class IndexOpenLogServiceImpl implements IndexOpenLogService {

    @Resource
    private IndexOpenLogMapper indexOpenLogMapper;


    @Override
    public boolean writeOpenLog(HttpServletRequest request) {
        //是首页打开的,记录信息
        IndexOpenLog indexOpenLog = new IndexOpenLog();

        indexOpenLog.setIp(request.getRemoteAddr());
        indexOpenLog.setTime(LocalDateTime.now());
        indexOpenLog.setUserAgent(request.getHeader("User-Agent"));
        indexOpenLog.setDelete(false);
        int insert = indexOpenLogMapper.insert(indexOpenLog);

        return insert > 0;
    }

    @Override
    public R getCountIndexOpen() {

        long count = indexOpenLogMapper.countByExample(null);

        return new R(Code.OK, "获取首页访问数量", count);
    }
}
